﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tenancy.Helper;
using System.Data;
using SQLServer.DBUtility;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_SubmitOrder 的摘要说明
    /// </summary>
    public class Hbung_SubmitOrder : IHttpHandler
    {

        /// <summary>
        /// 提交订单接口
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //用户登录的手机号
            string Hbung_Tel = context.Request.Params["Tel"];
            string Hbung_Phone = string.Empty;
            if (string.IsNullOrEmpty(Hbung_Tel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Tel为空"));
            }
            else
            {
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                Hbung_Phone = DESEncrypt.MD5Decrypt(Hbung_Tel);
                bool isMatch = cgsregex.IsMatch(Hbung_Phone);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Tel格式不正确"));
                    return;
                }
            }
            //签名
            string HBsign = context.Request.Params["Sign"];
            if (string.IsNullOrEmpty(HBsign))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Sign为空"));
                return;
            }
            else
            {
                if (GenerClass.IsOkMatch(Hbung_Phone) != Regex.Replace(HBsign, @"\s", "+"))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Sign不匹配"));
                    return;
                }
            }

            //产品类别 (工程机械/高空作业平台）
            string HB_ProductType = context.Request.Params["Type"];
            if (string.IsNullOrEmpty(HB_ProductType))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Type为空"));
                return;
            }
            else
            {
                if (HB_ProductType != "工程机械" && HB_ProductType != "高空作业平台")
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Type格式不对"));
                    return;
                }
            }

            //公司（个人）名字
            string Hb_ComPerName = context.Request.Params["ComPerName"];
            //if (string.IsNullOrEmpty(Hb_ComPerName))
            //{
            //    context.Response.Write(GenerClass.SetParamErrorJsData("ComPerName为空"));
            //    return;
            //}
            //公司（个人）地址
            string Hb_ComPerAddress = context.Request.Params["ComPerAddress"];
            //if (string.IsNullOrEmpty(Hb_ComPerAddress))
            //{
            //    context.Response.Write(GenerClass.SetParamErrorJsData("ComPerAddress为空"));
            //    return;
            //}

            //公司（个人）电话
            string Hb_ComPerPhone = context.Request.Params["ComPerPhone"];
            //if (string.IsNullOrEmpty(Hb_ComPerPhone))
            //{
            //    context.Response.Write(GenerClass.SetParamErrorJsData("ComPerPhone为空"));
            //    return;
            //}

           string NewAddress=string.Empty;

            //订单编号
            string Hb_OrderNum = string.Empty;

             //客户代码
            string HB_CustomerCode=string.Empty;
            //客户名字
            string HB_CName=string.Empty;
            //区别 业务员和区域经理
            string Hb_PeoType = string.Empty;
            //业务员（区域经理）工号 
            string HB_SalemanStaffNo=string.Empty;
            //业务员（区域经理）姓名
             string HB_SalemanName=string.Empty;
            //业务员（区域经理）区域
             string HB_SalemanRegion=string.Empty;
            //业务员（区域经理）电话
            string HB_SalemanTel=string.Empty;

            string HB_InStr = string.Format("select * from Hbung_CompanyInfo where CellTel='{0}'", Hbung_Phone);
                DataTable HB_ComInfo = DBHelpSQL.Query(HB_InStr).Tables[0];
                if (HB_ComInfo != null && HB_ComInfo.Rows.Count > 0)
                {
                    string CGS_User = string.Format("select CTel,SID,SName,SSex,SMobile,CCode,SRegion,CName,shortName,CAddress from View_CustomerForSaleman where RegisterMobilephone='{0}'", Hbung_Phone);
                    DataTable ZC_ComInfo = DbLocalSQL.Query(CGS_User).Tables[0];
                    if (ZC_ComInfo != null && ZC_ComInfo.Rows.Count > 0)
                    {
                        NewAddress=ZC_ComInfo.Rows[0]["CAddress"].ToString();
                        if (!string.IsNullOrEmpty(ZC_ComInfo.Rows[0]["CCode"].ToString()))
                        {
                            HB_CustomerCode = ZC_ComInfo.Rows[0]["CCode"].ToString();
                        }
                        if (!string.IsNullOrEmpty(ZC_ComInfo.Rows[0]["CName"].ToString()))
                        {
                            HB_CName = ZC_ComInfo.Rows[0]["CName"].ToString();
                        }
                        if (!string.IsNullOrEmpty(ZC_ComInfo.Rows[0]["SID"].ToString()))
                        {
                            Hb_PeoType = "业务员";
                            HB_SalemanStaffNo = ZC_ComInfo.Rows[0]["SID"].ToString();
                            if (!string.IsNullOrEmpty(ZC_ComInfo.Rows[0]["shortName"].ToString()))
                            {
                                string STRs = string.Format("select top 1 OrderNum from dbo.OnlineOrders where  convert(varchar(50),Creationtime,23)= convert(varchar(50),GETDATE(),23) order by Creationtime desc");
                                DataTable OrderS = DbLocalSQL.Query(STRs).Tables[0];
                                if (OrderS != null && OrderS.Rows.Count > 0)
                                {
                                    int cgs = Convert.ToInt32(OrderS.Rows[0]["OrderNum"].ToString().Substring(9, 5)) + 1;
                                    //M
                                    if (HB_ProductType == "工程机械")
                                    {
                                        Hb_OrderNum = "M" + DateTime.Now.ToString("yyyyMMdd") + cgs.ToString().PadLeft(5, '0') + ZC_ComInfo.Rows[0]["shortName"].ToString();
                                    }
                                    //A
                                    if (HB_ProductType == "高空作业平台")
                                    {
                                        Hb_OrderNum = "A" + DateTime.Now.ToString("yyyyMMdd") + cgs.ToString().PadLeft(5, '0') + ZC_ComInfo.Rows[0]["shortName"].ToString();
                                    }
                                }
                                else
                                {
                                    //M
                                    if (HB_ProductType == "工程机械")
                                    {
                                        Hb_OrderNum = "M" + DateTime.Now.ToString("yyyyMMdd") + "00001" + ZC_ComInfo.Rows[0]["shortName"].ToString();
                                    }
                                    //A
                                    if (HB_ProductType == "高空作业平台")
                                    {
                                        Hb_OrderNum = "A" + DateTime.Now.ToString("yyyyMMdd") + "00001" + ZC_ComInfo.Rows[0]["shortName"].ToString();
                                    }
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(ZC_ComInfo.Rows[0]["SName"].ToString()))
                        {
                            HB_SalemanName = ZC_ComInfo.Rows[0]["SName"].ToString();
                        }
                        if (!string.IsNullOrEmpty(ZC_ComInfo.Rows[0]["SRegion"].ToString()))
                        {
                            HB_SalemanRegion = ZC_ComInfo.Rows[0]["SRegion"].ToString();
                        }
                        if (!string.IsNullOrEmpty(ZC_ComInfo.Rows[0]["SMobile"].ToString()))
                        {
                            HB_SalemanTel = ZC_ComInfo.Rows[0]["SMobile"].ToString();
                        }

                    }
                }
                else
                {
                    context.Response.Write(GenerClass.SetChangeErrorJsData("用户不存在"));
                    return;
                }

            //设备使用地址
            string HB_MachineUseAddress = context.Request.Params["MacUseAddress"];
            if (string.IsNullOrEmpty(HB_MachineUseAddress))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("MacUseAddress不能为空"));
                return;
            }
            //如果是新用户就这个if
            if(string.IsNullOrEmpty(HB_SalemanStaffNo))
            {
                NewAddress=HB_ComInfo.Rows[0]["Address"].ToString();
               string[] strs=new string[]{};
               strs = HB_MachineUseAddress.Split('-');
                for(int i=0;i<strs.Length;i++)
                {
                    string j = strs[i].ToString();
                    string AddStr = string.Format("select * from RegionManager where Region like '%{0}%'  ",j);
                    DataTable MyAddress = DbLocalSQL.Query(AddStr).Tables[0];
                    if(MyAddress!=null&&MyAddress.Rows.Count>0)
                    {
                        Hb_PeoType = "区域经理";
                        if (!string.IsNullOrEmpty(MyAddress.Rows[0]["ID"].ToString()))
                        {
                            HB_SalemanStaffNo = MyAddress.Rows[0]["ID"].ToString();
                        }
                        if (!string.IsNullOrEmpty(MyAddress.Rows[0]["ManagerName"].ToString()))
                        {
                            HB_SalemanName = MyAddress.Rows[0]["ManagerName"].ToString();
                        }
                        if (!string.IsNullOrEmpty(MyAddress.Rows[0]["Region"].ToString()))
                        {
                            HB_SalemanRegion = MyAddress.Rows[0]["Region"].ToString();
                        }
                        if (!string.IsNullOrEmpty(MyAddress.Rows[0]["ManagerTel"].ToString()))
                        {
                            HB_SalemanTel = MyAddress.Rows[0]["ManagerTel"].ToString();
                        }
                        if (!string.IsNullOrEmpty(MyAddress.Rows[0]["shortName"].ToString()))
                        {
                            string STRs =  string.Format("select top 1 OrderNum from dbo.OnlineOrders where  convert(varchar(50),Creationtime,23)= convert(varchar(50),GETDATE(),23) order by Creationtime desc");
                            DataTable OrderS = DbLocalSQL.Query(STRs).Tables[0];
                            if (OrderS != null && OrderS.Rows.Count > 0)
                            {
                                int cgs = Convert.ToInt32(OrderS.Rows[0]["OrderNum"].ToString().Substring(9, 5))+1;

                                //M
                                if (HB_ProductType == "工程机械")
                                {
                                    Hb_OrderNum = "M" + DateTime.Now.ToString("yyyyMMdd") + cgs.ToString().PadLeft(5,'0') + MyAddress.Rows[0]["shortName"].ToString();
                                }
                                //A
                                if (HB_ProductType == "高空作业平台")
                                {
                                    Hb_OrderNum = "A" + DateTime.Now.ToString("yyyyMMdd") + cgs.ToString().PadLeft(5, '0') + MyAddress.Rows[0]["shortName"].ToString();
                                }
                            }
                            else
                            {
                                //M
                                if (HB_ProductType == "工程机械")
                                {
                                    Hb_OrderNum = "M" + DateTime.Now.ToString("yyyyMMdd") + "00001" + MyAddress.Rows[0]["shortName"].ToString();
                                }
                                //A
                                if (HB_ProductType == "高空作业平台")
                                {
                                    Hb_OrderNum = "M" + DateTime.Now.ToString("yyyyMMdd") + "00001" + MyAddress.Rows[0]["shortName"].ToString();
                                }
                            }
                        }
                   
                        
                    }
                }
            }

            if (string.IsNullOrEmpty(Hb_OrderNum))
            {
                context.Response.Write(GenerClass.SetChangeErrorJsData("订单号未生成"));
                return;
            }

            //联系人
            string HBName = context.Request.Params["Name"];
            if (string.IsNullOrEmpty(HBName))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Name为空"));
                return;
            }
            else
            {
                if (HBName.Length > 40)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Name长度过长"));
                    return;
                }
            }
            string NewName = string.Empty;
            //如果个人资料里面的联系人名字是空
            if(string.IsNullOrEmpty(HB_ComInfo.Rows[0]["Name"].ToString()))
            {
                NewName = HBName;
            }
            else
            {
                NewName = HB_ComInfo.Rows[0]["Name"].ToString();
            }

            //联系电话
            string HB_cgsTel = context.Request.Params["Telphone"];
            if (string.IsNullOrEmpty(HB_cgsTel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Telphone为空"));
                return;
            }
            else
            {
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                bool isMatch = cgsregex.IsMatch(HB_cgsTel);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Telphone格式不对"));
                    return;
                }
            }

            string NewTel = string.Empty;
            //如果个人资料里面的手机号是空
            if (string.IsNullOrEmpty(HB_ComInfo.Rows[0]["ConTactTel"].ToString()))
            {
                NewTel = HB_cgsTel;
            }
            else
            {
                NewTel = HB_ComInfo.Rows[0]["ConTactTel"].ToString();
            }
            #region 参数
            //品牌
            string HB_PBrands = context.Request.Params["Brand"];
            if (string.IsNullOrEmpty(HB_PBrands))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Brand为空"));
                return;
            }
            //机具类型 液压挖掘机/滑移装载机/推土机/剪刀式，曲臂式，直臂式和其它
            string HB_EquipmentType = context.Request.Params["EType"];
            if (string.IsNullOrEmpty(HB_EquipmentType))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("EType为空"));
                return;
            }
            //机型
            string HB_MacModel = context.Request.Params["Version"];
            if (string.IsNullOrEmpty(HB_MacModel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Version为空"));
                return;
            }
            //机编号
            string HB_PMachineNo = context.Request.Params["MacNum"];
            if (string.IsNullOrEmpty(HB_PMachineNo))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("MacNum为空"));
                return;
            }
            //停放省
            string HB_ParkProvince = context.Request.Params["Province"];
            if (string.IsNullOrEmpty(HB_ParkProvince))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Province为空"));
                return;
            }
            //停放市
            string HB_ParkCity = context.Request.Params["City"];
            if (string.IsNullOrEmpty(HB_ParkCity))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("City为空"));
                return;
            }
            //停放区/县
            string HB_ParkCounty = context.Request.Params["County"];
            if (string.IsNullOrEmpty(HB_ParkCounty))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("County为空"));
                return;
            }
            //停放详细地址
            string HB_ParkDetail = context.Request.Params["Detail"];
            if (string.IsNullOrEmpty(HB_ParkDetail))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Detail为空"));
                return;
            }
            //租赁开始时间
            string HB_RentStartDate = context.Request.Params["StartDate"];
            DateTime SDate = DateTime.Now;
            if (string.IsNullOrEmpty(HB_RentStartDate))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("StartDate不能为空"));
                return;
            }
            else
            {
                if (!DateTime.TryParse(HB_RentStartDate, out SDate))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("StartDate格式不对"));
                    return;
                }
            }
            //租赁结束日期
            string HBEndDate = context.Request.Params["EndDate"];
            DateTime Edate = DateTime.Now;
            if (string.IsNullOrEmpty(HBEndDate))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("EndDate不能为空"));
                return;
            }
            else
            {
                if (!DateTime.TryParse(HBEndDate, out Edate))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("EndDate格式不对"));
                    return;
                }
            }

            //是否需要操作手 1需要 0不需要
            string HB_IsOperator = context.Request.Params["Operator"];
            if (string.IsNullOrEmpty(HB_IsOperator))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Operator不能为空"));
                return;
            }
            else
            {
                if (HB_IsOperator != "0" && HB_IsOperator != "1")
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Operator格式不对"));
                    return;
                }
            }
            //收取操作手费用
            string HB_OperatorCost = context.Request.Params["OperatorCost"];
            decimal OCost = 0m;
            if (string.IsNullOrEmpty(HB_OperatorCost))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("OperatorCost不能为空"));
                return;
            }
            else
            {
                if (!decimal.TryParse(HB_OperatorCost, out OCost))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("OperatorCost格式不对"));
                    return;
                }
            }
            //是否需要加油 1需要 0不需要
            string HB_IsRefuel = context.Request.Params["Refuel"];
            if (string.IsNullOrEmpty(HB_IsRefuel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Refuel不能为空"));
                return;
            }
            else
            {
                if (HB_IsRefuel != "0" && HB_IsRefuel != "1")
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Refuel格式不对"));
                    return;
                }
            }
            // 加油费用
            string HB_RefuelCost = context.Request.Params["RefuelCost"];
            decimal RCost = 0m;
            if (string.IsNullOrEmpty(HB_RefuelCost))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("RefuelCost不能为空"));
                return;
            }
            else
            {
                if (!decimal.TryParse(HB_RefuelCost, out RCost))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("RefuelCost格式不对"));
                    return;
                }
            }
            //租赁方式 （天/周/月）
            string HB_RentWay = context.Request.Params["RentWay"];
            if (string.IsNullOrEmpty(HB_RentWay))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("RentWay不能为空"));
                return;
            }

            //租金优惠金额 （线上）
            string HB_FavorableForOnline = context.Request.Params["FavorableForOnline"];
            decimal FOnline = 0m;

            //商品id
            string HB_SPID = context.Request.Params["SPID"];
            if (string.IsNullOrEmpty(HB_SPID))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("SPID不能为空"));
                return;
            }

            //订单状态，此参数不需要传
            string OrderStatus = string.Empty;
            //租金单价 (-1,0)
            string HB_EveryPrice = context.Request.Params["RentUnitPrice"];
            decimal EPrice = 0m;
            if (!string.IsNullOrEmpty(HB_EveryPrice))
            {
                if (HB_EveryPrice == "0" || HB_EveryPrice == "-1")
                {
                    OrderStatus = "3";
                }
                else
                {
                    OrderStatus = "0";
                }
                if (!decimal.TryParse(HB_EveryPrice, out EPrice))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("RentUnitPrice格式不对"));
                    return;
                }
            }
            else
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("RentUnitPrice不能为空"));
                return;
            }
            //押金
            decimal CDeposit = 0m;
            if (!string.IsNullOrEmpty(HB_EveryPrice))
            {
                if (HB_ProductType == "工程机械")
                {
                    if (HB_EveryPrice != "-1" && HB_EveryPrice != "0")
                    {
                        CDeposit = EPrice;
                    }
                }
                if (HB_ProductType == "高空作业平台")
                {

                }
            }

            //租金总价
            string HB_RentTotalPrice = context.Request.Params["RentTotalPrice"];
            decimal TPrice = 0m;
            if (!string.IsNullOrEmpty(HB_RentTotalPrice))
            {
                if (!decimal.TryParse(HB_RentTotalPrice, out TPrice))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("RentTotalPrice格式不对"));
                    return;
                }
            }
            //系统计算运费(未优惠)
            string HB_FreightToAuto = context.Request.Params["FreightToAuto"];
            decimal FToAuto = 0m;
            if (string.IsNullOrEmpty(HB_FreightToAuto))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("FreightToAuto不能为空"));
                return;
            }
            else
            {
                if (!decimal.TryParse(HB_FreightToAuto, out FToAuto))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("FreightToAuto格式不对"));
                    return;
                }
            }
            //系统计算运费(优惠后) 暂时没有
            string HB_FreightToFinal = context.Request.Params["FreightToFinal"];
            decimal FToFinal = 0m;



            //租金优惠金额 （线下）暂时没有
            //  string HB_FavorableForOffline = context.Request.Params["FavorableForOffline"];
            decimal FForOffline = 0m;

            //总金额(未优惠之前应付的金额)
            string HB_ToTAlPrice = context.Request.Params["ALLToTalPrice"];
            decimal AToTalPrice = 0m;
            if (string.IsNullOrEmpty(HB_ToTAlPrice))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("ALLToTalPrice不能为空"));
                return;
            }
            else
            {
                if (!decimal.TryParse(HB_ToTAlPrice, out AToTalPrice))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("ALLToTalPrice格式不正确"));
                    return;
                }
            }

            //运输方式 (自提/利星行承运)
            string HB_TransportType = context.Request.Params["TransportType"];
            if (string.IsNullOrEmpty(HB_TransportType))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("TransportType不能为空"));
                return;
            }
            //预计使用时间
            string HB_ScheduledUseTime = context.Request.Params["ScheduledUseTime"];
            DateTime YTime = DateTime.Now;
            if (string.IsNullOrEmpty(HB_ScheduledUseTime))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("ScheduledUseTime不能为空"));
                return;
            }
            else
            {
                if (!DateTime.TryParse(HB_ScheduledUseTime, out YTime))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("ScheduledUseTime格式不对"));
                    return;
                }
            } 
            #endregion
           

            string AddOrderStr = string.Format(@"insert into OnlineOrders (OrderNum,CustomerCode,CustomerName,CPhone,ProductType,PBrands,PEquipmentType,
            PMachineModel,PMachineNo,ParkProvince,ParkCity,ParkCounty,ParkDetail,RentStartDate,RentEndDate,MachineUseAddress,SLinkman,SLinkPhone,IsOperator,
            OperatorCost,IsRefuel,RefuelCost,RentWay,RentUnitPrice,RentTotalPrice,FreightToAuto,FreightToFinal,Deposit,TotalPrice,FavorableForOnline,FavorableForOffline,
            ExecutoryCost,OrderStatus,SalemanStaffNo,SalemanName,SalemanRegion,SalemanTel,TransportType,ScheduledUseTime,Creationtime,PeoType,DeviceDID,CAddress
             )
            values 
            ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}',
             '{27}','{28}','{29}','{30}','{31}','{32}','{33}','{34}','{35}','{36}','{37}','{38}',getdate(),'{39}','{40}','{41}')
            ", Hb_OrderNum, HB_CustomerCode, HB_CName, Hbung_Phone, HB_ProductType, HB_PBrands, HB_EquipmentType, HB_MacModel, HB_PMachineNo, HB_ParkProvince, HB_ParkCity, HB_ParkCounty, HB_ParkDetail, SDate, Edate, HB_MachineUseAddress,
             NewName, NewTel, HB_IsOperator, HB_OperatorCost, HB_IsRefuel, RCost, HB_RentWay, EPrice, TPrice, FToAuto, FToFinal, CDeposit, AToTalPrice, FOnline, FForOffline,
             (AToTalPrice - FOnline), OrderStatus, HB_SalemanStaffNo, HB_SalemanName, HB_SalemanRegion, HB_SalemanTel, HB_TransportType, YTime, Hb_PeoType, HB_SPID,NewAddress
              );
            if (DbLocalSQL.ExecuteSql(AddOrderStr)>0)
            {
                #region 推送消息
                //如果是议价，老用户给 业务员 ，行政，经理推送,新用户给区域经理推送
                if (OrderStatus == "3")
                {
                    if (HB_ComInfo != null && HB_ComInfo.Rows.Count > 0)
                    {
                        string ComName = string.Empty;
                        if (!string.IsNullOrEmpty(HB_ComInfo.Rows[0]["CompanyName"].ToString()))
                        {
                            if (HB_ComInfo.Rows[0]["CompanyName"].ToString().Length > 10)
                            {
                                ComName = HB_ComInfo.Rows[0]["CompanyName"].ToString().Substring(0, 9);
                            }
                        }

                        string CGS_User = string.Format("select CTel,SID,SName,SSex,SMobile,CCode,SRegion,CName,shortName,CAddress from View_CustomerForSaleman where RegisterMobilephone='{0}'", Hbung_Phone);
                        DataTable ZC_ComInfo = DbLocalSQL.Query(CGS_User).Tables[0];
                        if (ZC_ComInfo != null && ZC_ComInfo.Rows.Count > 0)
                        {
                            string QianStr = string.Format(@"update Hbung_CompanyInfo set CompanyName='{0}',Phone='{1}',Address='{2}',ServicePerson='{3}',Name='{5}',ConTactTel='{6}' where CellTel='{4}' ",
                                ZC_ComInfo.Rows[0]["CName"].ToString(), ZC_ComInfo.Rows[0]["CTel"].ToString(), ZC_ComInfo.Rows[0]["CAddress"].ToString(), ZC_ComInfo.Rows[0]["SName"].ToString(),
                                Hbung_Phone, NewName, NewTel
                                );
                            DbLocalSQL.ExecuteSql(QianStr);

                            //给老用户推送
                            if (!string.IsNullOrEmpty(ZC_ComInfo.Rows[0]["SMobile"].ToString()))
                            {
                                string OldStr = string.Format("select * from SalesAdminManger where SalesTel='{0}'", ZC_ComInfo.Rows[0]["SMobile"].ToString());
                                DataTable OldData = DbLocalSQL.Query(OldStr).Tables[0];
                                if (OldData != null && OldData.Rows.Count > 0)
                                {
                                    //行政推送消息
                                    if (!string.IsNullOrEmpty(OldData.Rows[0]["AdminTel"].ToString()))
                                    {
                                        Tenancy.Helper.MsgSend.HB_SendMsg(OldData.Rows[0]["AdminTel"].ToString(), GenerClass.MouldSMS(ComName, NewTel, HB_EquipmentType, SDate.ToString("yy/MM/dd"), Edate.ToString("yy/MM/dd"), HB_MachineUseAddress));
                                    }
                                    //区域经理推送消息
                                    if (!string.IsNullOrEmpty(OldData.Rows[0]["ManagerTel"].ToString()))
                                    {
                                        Tenancy.Helper.MsgSend.HB_SendMsg(OldData.Rows[0]["ManagerTel"].ToString(), GenerClass.MouldSMS(ComName, NewTel, HB_EquipmentType, SDate.ToString("yy/MM/dd"), Edate.ToString("yy/MM/dd"), HB_MachineUseAddress));
                                    }
                                }
                                //业务员推送消息
                                Tenancy.Helper.MsgSend.HB_SendMsg(ZC_ComInfo.Rows[0]["SMobile"].ToString(), GenerClass.MouldSMS(ComName, NewTel, HB_EquipmentType, SDate.ToString("yy/MM/dd"), Edate.ToString("yy/MM/dd"), HB_MachineUseAddress));
                            }
                        }

                        //给新用户推送
                        else
                        {

                            string QianStr = string.Format(@"update Hbung_CompanyInfo set ServicePerson='{0}',Name='{1}',ConTactTel='{2}' where CellTel='{3}' ", HB_SalemanName, NewName, NewTel, Hbung_Phone);
                            DBHelpSQL.ExecuteSql(QianStr);

                            Tenancy.Helper.MsgSend.HB_SendMsg(HB_SalemanTel, GenerClass.MouldSMS(ComName, NewTel, HB_EquipmentType, SDate.ToString("yy/MM/dd"), Edate.ToString("yy/MM/dd"), HB_MachineUseAddress));
                        }
                    }
                } 
                #endregion


                string SOrders = string.Format("select OrderNum,PeoType,SalemanName,SalemanRegion,SalemanTel from OnlineOrders where OrderNum='{0}'", Hb_OrderNum);
                DataTable HB_OrderInfo = DbLocalSQL.Query(SOrders).Tables[0];
                string Hb_DateJson = JsonHelper.DataTableToJSON(HB_OrderInfo);
                context.Response.Write(GenerClass.SetSucessJsData("发布成功", Hb_DateJson));
            }
            else
            {
                context.Response.Write(GenerClass.SetChangeErrorJsData("发布失败"));
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}